package genetic.q4.functions;

import genetic.Function;
import genetic.TreeNode;
import genetic.q4.TicTacToeBoard;

public class SwitchX extends TicTacToeFunction{

	int x;

	/**
	 * constructor for prototype only (NOT TO BE USED)
	 */
	public SwitchX() {
		super(null);
	}

	public SwitchX(int x, TreeNode doClear, TreeNode doMine, TreeNode doOther) {
		super(new TreeNode[]{doClear, doMine, doOther});
		this.x = x;
	}

	@Override
	public Function createInstance(TreeNode[] kids) {
		return new SwitchX(x, kids[0], kids[1], kids[2]);
	}

	@Override
	public int getNumKids() {
		return 3;
	}

	@Override
	public TreeNode createRandomInstance(TreeNode[] kids) {
		int rand = (int)(Math.random()*9.0);
		return new SwitchX(rand, kids[0], kids[1], kids[2]);
	}

	@Override
	public void play(TicTacToeBoard board, int side) {
		TreeNode exec = null;
		switch(board.get(x)){
		case TicTacToeBoard.CLEAR:
			exec = kids[0];
			break;
		case TicTacToeBoard.X:
			exec = (side == TicTacToeBoard.X ? kids[1]: kids[2]);
			break;
		case TicTacToeBoard.O:
			exec = (side == TicTacToeBoard.X ? kids[2]: kids[1]);
			break;
		}
		if (exec.isFunction()){
			((TicTacToeFunction)exec).play(board, side);
		} else {
			((TicTacToeTerminal)exec).play(board, side);
		}
	}

	public int getIndex() {
		return x;
	}

	@Override
	public String getName() {
		return "s" + x;
	}

}
